﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace HR60_PanelSoft.View.Base
{
	/// <summary>
	/// Form控件基类，与BaseForm配合实现Form内的控件数据获取及自动提交
	/// 在BaseFormControl嵌套的情况下，只有顶层的Control有效
	/// </summary>
	public class BaseFormControl : BaseData
	{
		private string _Id = null;

		private object _Value = null;

		private bool _ValueEnabled = true;

		private bool _AutoSubmitFlag = true;

		private bool _CatchValueChange = true;

		private string _ValidRegx = "";

		private int _Min = -1;

		private int _Max = -1;

		/// <summary>
		/// Value发生改变后的事件，并非所有控件都要实现这个机制，视情况而定
		/// </summary>
		[Browsable(true)]
		public event EventHandler ValueChanged;


		/// <summary>
		/// 控件id，提交Form时作为Key
		/// </summary>
		[Browsable(true)]
		[Description("id"), Category("String"), DefaultValue("")]
		public virtual string Id
		{
			get
			{
				return this._Id;
			}
			set
			{
				this._Id = value;
			}
		}

		/// <summary>
		/// 控件取值，提交Form时作为Value
		/// </summary>
		public virtual object Value
		{
			get
			{
				return this._Value;
			}
			set
			{
				this._Value = value;
			}
		}

		/// <summary>
		/// 值是否有效，若无效则在提交时忽略此数据字段
		/// </summary>
		public virtual bool ValueEnabled
		{
			get
			{
				return _ValueEnabled;
			}
			set
			{
				_ValueEnabled = value;
			}
		}

		/// <summary>
		/// 是否自动提交的标志，如果为false则不会触发Form自动提交流程
		/// </summary>
		[Browsable(true)]
		[Description("AutoSubmitFlag"), Category("Boolean"), DefaultValue(true)]
		public bool AutoSubmitFlag
		{
			get
			{
				return this._AutoSubmitFlag;
			}
			set
			{
				this._AutoSubmitFlag = value;
			}
		}

		/// <summary>
		/// 是否触发ValueChange事件，若false，也不会触发Form自动提交流程
		/// </summary>
		[Browsable(true)]
		[Description("CatchValueChange"), Category("Boolean"), DefaultValue(true)]
		public bool CatchValueChange
		{
			get
			{
				return this._CatchValueChange;
			}
			set
			{
				this._CatchValueChange = value;
			}
		}

		/// <summary>
		/// 数据校验正则
		/// </summary>
		[Browsable(true)]
		[Description("ValidRegx"), Category("String"), DefaultValue("")]
		public virtual string ValidRegx
		{
			get
			{
				return this._ValidRegx;
			}
			set
			{
				this._ValidRegx = value;
			}
		}

		/// <summary>
		/// 字符串类型为最小长度，数值型为最小值
		/// </summary>
		[Browsable(true)]
		[Description("Min"), Category("Int32"), DefaultValue(-1)]
		public virtual int Min
		{
			get
			{
				return this._Min;
			}
			set
			{
				this._Min = value;
			}
		}

		/// <summary>
		/// 字符串类型为最大长度，数值型为最大值
		/// </summary>
		[Browsable(true)]
		[Description("Max"), Category("Int32"), DefaultValue(-1)]
		public virtual int Max
		{
			get
			{
				return this._Max;
			}
			set
			{
				this._Max = value;
			}
		}

		/// <summary>
		/// IFromControl的Value发生改变时触发执行
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected virtual void valueChange(object sender, EventArgs e)
		{
			// 判断是否触发ValueChanged事件
			if (!CatchValueChange)
			{
				return;
			}
			if (ValueChanged != null)
			{
				ValueChanged(this, e);
			}
			
			// 判断是否执行自动提交流程
			if (!AutoSubmitFlag) return;

			Control control = (Control)sender;
			if (control.Parent == null) return;

			while (!control.Parent.GetType().IsSubclassOf(typeof(BaseForm)))
			{
				control = control.Parent;
				if (typeof(BaseFormControl).IsAssignableFrom(control.GetType()))
				{
					// 如果存在上一层BaseFormContrl，则传递触发valueChange，并返回
					((BaseFormControl)control).valueChange(this, e);
					return;
				}
				if (control.Parent == null) return;
			}
			BaseForm form = (BaseForm)control.Parent;
			form.doFormChange((BaseFormControl)sender, e);
		}

		/// <summary>
		/// 查找控件所在Form
		/// </summary>
		/// <returns></returns>
		protected BaseForm findForm()
		{
			Control control = this;
			while (!control.Parent.GetType().IsSubclassOf(typeof(BaseForm)))
			{
				control = control.Parent;
				if (control.Parent == null) return null;
			}
			return (BaseForm)control.Parent;
		}

		/// <summary>
		/// 查找控件所在FormControl
		/// </summary>
		/// <returns></returns>
		protected BaseFormControl findParentFormControl()
		{
			Control control = this;
			while (!control.Parent.GetType().IsSubclassOf(typeof(BaseFormControl)))
			{
				control = control.Parent;
				if (control.Parent == null) return null;
			}
			return (BaseFormControl)control.Parent;
		}
		/// <summary>
		/// 查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls()
		{
			return findChildFormControls(null);
		}


		/// <summary>
		/// 通过id查找控件列表
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		public IList<BaseFormControl> findChildFormControls(string id)
		{
			IList<BaseFormControl> list = new List<BaseFormControl>();
			BaseForm.findChildFormControls(id, list, this);
			return list;
		}
	}
}
